package cn.edu.nuc.fourth.controller;

import java.io.File;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import cn.edu.nuc.fourth.dto.PageInfo;
import cn.edu.nuc.fourth.model.Aproject;
import cn.edu.nuc.fourth.model.Project;
import cn.edu.nuc.fourth.model.School;
import cn.edu.nuc.fourth.model.User;
import cn.edu.nuc.fourth.services.interfaces.FillingService;

@RequestMapping("/school")
@Controller
public class SchoolController {

	@Autowired
	private FillingService service;

	/**
	 * 跳转填报办学
	 * 
	 * @return
	 */
	@RequestMapping("/school")
	public String initadd(HttpSession session, Model model) {

		// 查询看看有没有2016年的数据

		School school = service.noAddSchool("-1");
		// 大于0相当于还没有填报
		if (school == null) {

			return "schooluser/schoolAdd";
		} else {

			model.addAttribute("school", school);
			if (school.getState() == 1) {

				return "schooluser/noeditSchool";
			} else {

				return "schooluser/oneSchool";
			}
		}

	}

	/**
	 * 执行添加
	 * 
	 * @param school
	 * @return
	 */
	@RequestMapping(value = "/schooladd", method = RequestMethod.POST)
	public String add(@Valid @ModelAttribute("school") School school, Errors errors, Model model) {

		if (errors.hasErrors()) {
			System.out.println("校验办学填报");
			return "schooluser/schoolAdd";
		}
		Integer result = service.schoolAdd(school);
		return "总体界面";
	}

	@RequestMapping(value = "/schoollist")
	public String schoolList(Integer userid, @RequestParam(name = "current", defaultValue = "1") Integer current,
			Model model) {

		PageInfo pageInfo = service.schoolList(-1, current);
		PageInfo pageMajor = service.majorList(-1, 1);
		model.addAttribute("page", pageInfo);
		model.addAttribute("pagemajor", pageMajor);
		return "schooluser/school";
	}

	/**
	 * school列表之后的查询
	 * 
	 * @param sid
	 * @return
	 */
	@RequestMapping("/oneschool")
	public String oneSchool(Integer sid, Model model) {

		School school = service.oneSchool(sid);
		model.addAttribute("school", school);
		model.addAttribute("s", "false");
		if (school.getState() == 1) {

			return "schooluser/noeditSchool";
		}

		return "schooluser/oneSchool";
	}

	@RequestMapping("/schooledit")
	public String editSchool(@ModelAttribute("s") School s, Model model) {

		System.out.println("s:" + s);

		School school = service.schoolEdit(s);
		// System.out.println("school:" + school);
		model.addAttribute("s", "true");
		model.addAttribute("school", school);

		return "schooluser/oneSchool";
	}

	@RequestMapping(value = "/schooldelete", method = RequestMethod.GET)
	public String deleteSchool(Integer sid) {

		Integer result = service.schoolDelete(sid);
		return "redirect:/school/schoollist";
	}

	@RequestMapping("/noedit")
	public String noeditSchool(Integer sid, Integer state, Model model) {

		School school = service.noeditSchool(sid, state);

		model.addAttribute("school", school);
		return "schooluser/noeditSchool";
	}

	@RequestMapping(value = "/ajaxschool", method = RequestMethod.POST)
	public @ResponseBody String ajaxSchool() {

		return null;
	}

	@RequestMapping(value = "/ajax", method = RequestMethod.POST)
	public @ResponseBody PageInfo ajaxList(Integer userid, Integer current) {

		PageInfo pageInfo = service.schoolList(userid, current);
		return pageInfo;
	}

	/**
	 * 跳转 项目填报
	 * 
	 * @return
	 */
	@RequestMapping("/forproject")
	public String initproject() {

		return "schooluser/projectAdd";
	}

	@RequestMapping(value = "/projectadd", method = RequestMethod.POST)
	public @ResponseBody String projectadd(MultipartFile main, MultipartFile province, MultipartFile city,
			MultipartFile onther, HttpServletRequest request, Project project, HttpSession session) {

		/*
		 * session.getAttribute("user"); User user = new User();
		 */
		String path = request.getServletContext().getRealPath("/WEB-INF/schooluser/");
		path += "-1\\";
		System.out.println(path);
		service.projectadd(main, province, city, onther, project, path);
		return "Ok";

	}

	/**
	 * 用户下载页面跳转
	 * 
	 * @return
	 */
	@RequestMapping("/fordownload")
	public String initdown(Model model) {

		Aproject aproject = service.projectlist();
		model.addAttribute("aproject", aproject);
		return "schooluser/userDownload";
	}

	@RequestMapping(value = "/userdownload")
	public ResponseEntity<byte[]> download(HttpServletRequest request, String path, String filename)
			throws IOException {

		File file = new File(path, filename);
		HttpHeaders header = new HttpHeaders();
		String fname = new String(filename.getBytes("UTF-8"), "iso-8859-1");
		header.setContentDispositionFormData("attachment", fname);
		header.setContentType(MediaType.APPLICATION_OCTET_STREAM);

		return new ResponseEntity<byte[]>(FileUtils.readFileToByteArray(file), header, HttpStatus.CREATED);
	}

	/**
	 * 用户项目列表展示
	 * 
	 * @return
	 */
	@RequestMapping("/projectlist")
	public String forproject(Model model) {

		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		model.addAttribute("year", year);
		return "schooluser/project";
	}

	@RequestMapping("/oneproject")
	public String dateprojectlist(String userid, String date, Model model) {

		List<Project> list = service.dateprojectlist("-1", date);
		String d = null;
		Integer state = 0;
		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			d = p.getPdate();
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
			state = p.getState();
		}
		model.addAttribute("pdate", d);
		model.addAttribute("date", date);

		if (state == 1) {

			return "schooluser/noeditProject";
		}

		return "schooluser/oneProject";
	}

	@RequestMapping("/deleteproject")
	public String deleteproject(String userid,Integer projectid, String date, Model model) {

		List<Project> list = service.deleteproject(projectid, userid, date);
		String d = null;
		String checkPer = null;
		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			d = p.getPdate();
			checkPer = p.getCheckPer();
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
			if (p.getState() == 1) {

				return "redirect:/school/noeditproject";
			}
		}
		model.addAttribute("pdate", d);
		model.addAttribute("date", date);
		model.addAttribute("checkPer", checkPer);
		return "schooluser/oneProject";
	}

	/**
	 * 进行项目修改
	 * 
	 * @return
	 */
	@RequestMapping("/oneupload")
	public String oneupload(MultipartFile file, Project project, HttpServletRequest request, HttpSession session,
			Model model) {

		System.out.println("project:" + project);
		String path = request.getServletContext().getRealPath("/WEB-INF/schooluser/");
		path += "-1\\";
		List<Project> list = service.oneupload(file, path, project);
		String d = null;
		String checkPer = null;
		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			d = p.getPdate();
			checkPer = p.getCheckPer();
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
		}
		model.addAttribute("pdate", d);
		model.addAttribute("date", project.getDate());
		model.addAttribute("checkPer", checkPer);
		return "schooluser/oneProject";
	}

	@RequestMapping(value = "noeditproject", method = RequestMethod.POST)
	public String noeditproject(String userid, String date, Model model) {

		List<Project> list = service.noeditproject(userid, date);

		String d = null;
		for (int i = 0; i < list.size(); i++) {

			Project p = list.get(i);
			d = p.getPdate();
			if (p.getProjecttype().equals("main")) {
				model.addAttribute("main", p);
			}
			if (p.getProjecttype().equals("province")) {

				model.addAttribute("province", p);
			}
			if (p.getProjecttype().equals("city")) {

				model.addAttribute("city", p);
			}
			if (p.getProjecttype().equals("onther")) {

				model.addAttribute("onther", p);
			}
		}
		model.addAttribute("pdate", d);
		model.addAttribute("date", date);
		return "schooluser/noeditProject";
	}
}
